import { Message } from 'element-ui'
/**
 * @author：mxj
 * @function:根据百源账号下拉框远程搜索，默认带出前10条，剩下的远程搜索下拉
*/
const byAccountOptionsFun = function (queryString) {
    return new Promise((resolve) => {
        if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{3,}$/.test(queryString) || queryString == '') {
            this.$httpExt().post('/dev-basp-user/account/selectEmpByUsername', {
                pageNum: 1,
                pageSize: 10,
                username: queryString
            }).then(function (response) {
                const results = response.result.data
                for (var i = 0; i < results.length; i++) {
                    var obj = {
                        label: results[i].username,
                        id: results[i].byAccountId,
                        code: results[i].empCode
                    }
                    results[i] = obj
                }
                resolve(results)
            }, function (response) {
                Message({
                    message: response.msg,
                    type: 'warning'
                })
            })
        } else {
            resolve([])
        }
    })
}
/**
 * @author：mxj
 * @function:查询子系统
*/
const getSubSystemList = function () {
    return new Promise((resolve) => {
        var that = this
        var arr = []
        that.$httpExt().get('/dev-basp-user/modules/querySubsys').then(function (response) {
            for (var i = 0; i < response.result.length; i++) {
                arr.push({
                    value: response.result[i].subsysCode,
                    label: response.result[i].moduleName
                })
            }
            resolve(arr)
        }, function () {
        })
    })
}
/**
 * @author：mxj
 * @function:根据子系统，百源账号，角色名称模糊下拉框远程搜索，默认带出前10条，剩下的远程搜索下拉
*/
const getroleListFun = function (queryString, subsysCode, byAccountId) {
    return new Promise((resolve) => {
        if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{3,}$/.test(queryString) || queryString == '') {
            this.$httpExt().post('/dev-basp-user/accountDataGroup/selectRole', {
                pageNum: 1,
                pageSize: 10,
                roleName: queryString,
                subsysCode: subsysCode,
                byAccountId: byAccountId
            }).then(function (response) {
                const results = response.result.data
                resolve(results)
            }, function (response) {
                Message({
                    message: response.msg,
                    type: 'warning'
                })
            })
        } else {
            resolve([])
        }
    })
}
/**
 * @author：mxj
 * @function:根据资源名称下拉框远程搜索,查询所有资源，默认带出前10条，剩下的远程搜索下拉
*/
const resourceOptionsAllFun = function (queryString, roleId) {
    return new Promise((resolve) => {
        if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{3,}$/.test(queryString) || queryString == '') {
            var that = this
            that.$httpExt().post('/dev-basp-user/accountDataGroup/selectAllResByName', {
                pageNum: 1,
                pageSize: 10,
                resourceName: queryString,
                roleId: roleId
            }).then(function (response) {
                const results = response.result.data
                for (var i = 0; i < results.length; i++) {
                    var obj = {
                        label: results[i].resourceName,
                        id: results[i].resourceId,
                        code: results[i].resourceCode,
                        ownerSysName: results[i].ownerSysName,
                        ownerSysId: results[i].ownerSysId,
                        paramStruct: results[i].paramStruct,
                        customDatagroup: results[i].customDatagroup
                    }
                    results[i] = obj
                }
                resolve(results)
            }, function (response) {
                Message({
                    message: response.msg,
                    type: 'warning'
                })
            })
        } else {
            resolve([])
        }
    })
}
/**
 * @author：mxj
 * @function:根据资源名称下拉框远程搜索，查询未授权资源，默认带出前10条，剩下的远程搜索下拉
*/
const resourceOptionsFun = function (queryString, roleId) {
    return new Promise((resolve) => {
        if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{3,}$/.test(queryString) || queryString == '') {
            var that = this
            that.$httpExt().post('/dev-basp-user/accountDataGroup/selectByResName', {
                pageNum: 1,
                pageSize: 10,
                resourceName: queryString,
                roleId: roleId
            }).then(function (response) {
                const results = response.result.data
                for (var i = 0; i < results.length; i++) {
                    var obj = {
                        label: results[i].resourceName,
                        id: results[i].resourceId,
                        code: results[i].resourceCode,
                        ownerSysName: results[i].ownerSysName,
                        ownerSysId: results[i].ownerSysId,
                        paramStruct: results[i].paramStruct,
                        customDatagroup: results[i].customDatagroup,
                        systemId: results[i].systemId,
                        paramUrl:results[i].paramUrl,
						paramType:results[i].paramType
                    }
                    results[i] = obj
                }
                resolve(results)
            }, function (response) {
                Message({
                    message: response.msg,
                    type: 'warning'
                })
            })
        } else {
            resolve([])
        }
    })
}
/**
 * @author：mxj
 * @function:根据数据组名称下拉框远程搜索，默认带出前10条，剩下的远程搜索下拉
*/
const dataGroupNameOptionsFun = function (queryString, resourceId) {
    return new Promise((resolve) => {
        if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{3,}$/.test(queryString) || queryString == '') {
            this.$httpExt().post('/dev-basp-user/accountDataGroup/queryRuleDataGroup', {
                pageNum: 1,
                pageSize: 10,
                resourceId: resourceId,
                dataGroupName: queryString
            }).then(function (response) {
                const results = response.result.data
                for (var i = 0; i < results.length; i++) {
                    var obj = {
                        label: results[i].dataGroupName,
                        id: results[i].dataGroupId
                    }
                    results[i] = obj
                }
                resolve(results)
            }, function (response) {
                Message({
                    message: response.msg,
                    type: 'warning'
                })
            })
        } else {
            resolve([])
        }
    })
}

const baspUtilRequest = {
    byAccountOptionsFun,
    getSubSystemList,
    getroleListFun,
    resourceOptionsAllFun,
    resourceOptionsFun,
    dataGroupNameOptionsFun
}
export default baspUtilRequest
